package com.shunbo.convenient.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shunbo.convenient.service.BlessingService;
import com.shunbo.convenient.service.ProductOrderService;
import com.shunbo.convenient.service.ProductService;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.Blessing;
import com.shunbo.convenient.vo.Product;
import com.shunbo.convenient.vo.ProductOrder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author weizhuang
 * 福袋
 */
@RestController
@RequestMapping("/blessing")
public class BlessingController {


    @Resource
    private BlessingService blessingService;
    @Resource
    private ProductService productService;
    @Resource
    private ProductOrderService productOrderService;

    /**
     * 福袋列表
     *
     * @param blessing 福袋
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getBlessingList(@RequestBody Blessing blessing) {
        try {
            if (blessing.getPageNo() == null) {
                blessing.setPageNo(1);
            }
            PageHelper.startPage(blessing.getPageNo(), 10);
            List<Blessing> all = blessingService.findAll(blessing);
            for (Blessing bless : all
            ) {
                List<String> ids = Arrays.asList(bless.getProductIds().split(","));
                Page<Product> byIds = productService.findByIds(ids);
                for (Product prod : byIds
                        ) {
                    ProductOrder productOrder = new ProductOrder();
                    Integer otherStock = 0;
                    productOrder.setPid(prod.getId());
                    List<ProductOrder> all1 = productOrderService.findAll(productOrder);
                    for (ProductOrder productOrders : all1
                            ) {
                        otherStock += productOrders.getNum();
                    }
                    int result = Integer.parseInt(prod.getStock()) - otherStock;
                    prod.setOtherStock(result + "");
                    prod.setVolume(otherStock);
                }
                bless.setProductList(byIds.getResult());
                ProductOrder productOrder = new ProductOrder();
                Integer otherStock = 0;
                productOrder.setBid(bless.getId());
                int result = Integer.parseInt(bless.getStock()) - otherStock;
                bless.setOtherStock(result + "");
                bless.setVolume(otherStock);
            }
            PageInfo<Blessing> pageInfo = new PageInfo<>(all);
            Map<String,Object> resultMap = new HashMap<>();
            resultMap.put("blessingList",all);
            resultMap.put("pageNo",blessing.getPageNo());
            resultMap.put("count",pageInfo.getTotal());
            return ConvenientResult.build(200, "成功", resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 添加福袋
     *
     * @param blessing 福袋
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveBlessing(@RequestBody Blessing blessing) {
        try {
            Double money  = 0.0;
            String[] productIds = blessing.getProductIds().split(",");
            Page<Product> byIds1 = productService.findByIds(Arrays.asList(productIds));
            for (Product Product : byIds1.getResult()
                    ) {
                money += Product.getMoney();
            }
            String profit = blessing.getProfit().substring(0,blessing.getProfit().length() - 1);
            double v = money / 100 * (100 - Double.parseDouble(profit));
            System.out.println(v);
            if( v < 30 ){
                return ConvenientResult.build(400, "添加失败,福袋价格不能低于30元", false);
            }
            DecimalFormat df = new DecimalFormat("######0"); //四色五入转换成整数

            String format = df.format(v);
            blessing.setMoney(Double.parseDouble(format));
            Integer save = blessingService.save(blessing);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getMessage());
        }
    }

    /**
     * 更新福袋
     *
     * @param blessing 福袋
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateBlessing(@RequestBody Blessing blessing) {
        try {
            Double money  = 0.0;
            String[] productIds = blessing.getProductIds().split(",");
            Page<Product> byIds1 = productService.findByIds(Arrays.asList(productIds));
            for (Product Product : byIds1.getResult()
                    ) {
                money += Product.getMoney();
            }
            String profit = blessing.getProfit().substring(0,blessing.getProfit().length() - 1);
            double v = money / 100 * (100 - Double.parseDouble(profit));
            if( v < 30.0 ){
                return ConvenientResult.build(400, "添加失败,福袋价格不能低于30元", false);
            }
            DecimalFormat df = new DecimalFormat("######0"); //四色五入转换成整数

            String format = df.format(v);
            blessing.setMoney(Double.parseDouble(format));
            Integer save = blessingService.update(blessing);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 删除福袋
     *
     * @param blessing 福袋
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteBlessing(@RequestBody Blessing blessing) {
        try {
            Integer save = blessingService.delete(blessing.getId() + "");
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * id查福袋
     *
     * @param blessing 福袋
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getBlessingById(@RequestBody Blessing blessing) {
        try {
            Blessing st = blessingService.findById(blessing.getId() + "");
            List<String> ids = Arrays.asList(st.getProductIds().split(","));
            Page<Product> byIds = productService.findByIds(ids);
            st.setProductList(byIds.getResult());
            return ConvenientResult.build(200, "成功", st);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }


}
